﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#include "GnssLogs.h"
#include "Decoders/ALIGNBSLNENUDecoder.h"
#include "Decoders/ALIGNBSLNXYZDecoder.h"
#include "Decoders/ALIGNDOPDecoder.h"
#include "Decoders/ALMANACDecoder.h"
#include "Decoders/ALMDecoder.h"
#include "Decoders/AUTHCODESDecoder.h"
#include "Decoders/AVEPOSDecoder.h"
#include "Decoders/BDSEPHEMERISDecoder.h"
#include "Decoders/BESTPOSDecoder.h"
#include "Decoders/BESTSATSDecoder.h"
#include "Decoders/BESTVELDecoder.h"
#include "Decoders/GGADecoder.h"
#include "Decoders/GLLDecoder.h"
#include "Decoders/GPSCNAVRAWMESSAGEDecoder.h"
#include "Decoders/GPSEPHEMDecoder.h"
#include "Decoders/GRSDecoder.h"
#include "Decoders/GSADecoder.h"
#include "Decoders/GSTDecoder.h"
#include "Decoders/GSVDecoder.h"
#include "Decoders/HDTDecoder.h"
#include "Decoders/PSRDOP2Decoder.h"
#include "Decoders/PSRPOSDecoder.h"
#include "Decoders/RAIMSTATUSDecoder.h"
#include "Decoders/RANGEDecoder.h"
#include "Decoders/RMBDecoder.h"
#include "Decoders/RMCDecoder.h"
#include "Decoders/RTKDOPDecoder.h"
#include "Decoders/SATXYZ2Decoder.h"
#include "Decoders/SBAS25Decoder.h"
#include "Decoders/TIMEDecoder.h"
#include "Decoders/VTGDecoder.h"
#include "Decoders/ZDADecoder.h"

#define REGISTER_DECODER(NAMESPACE)                                                                                                        \
    template<>                                                                                                                             \
    GNSSLOGS_EXPORT std::optional<##NAMESPACE::Data> GnssLogs::Decoder::decode<##NAMESPACE::Data>(std::string_view source) const noexcept  \
    {                                                                                                                                      \
        try                                                                                                                                \
        {                                                                                                                                  \
            GnssLogs::##NAMESPACE::Data data;                                                                                              \
            return tao::pegtl::parse<GnssLogs::##NAMESPACE##Decoder::Expression, GnssLogs::##NAMESPACE##Decoder::Action>(                  \
                       tao::pegtl::string_input(source, ""), data)                                                                         \
                       ? std::optional { data }                                                                                            \
                       : std::nullopt;                                                                                                     \
        }                                                                                                                                  \
        catch (...)                                                                                                                        \
        {                                                                                                                                  \
            return std::nullopt;                                                                                                           \
        }                                                                                                                                  \
    }

namespace GnssLogs
{
REGISTER_DECODER(GST)
REGISTER_DECODER(GSV)
REGISTER_DECODER(GRS)
REGISTER_DECODER(GSA)
REGISTER_DECODER(RANGE)
REGISTER_DECODER(GGA)
REGISTER_DECODER(ALM)
REGISTER_DECODER(GLL)
REGISTER_DECODER(HDT)
REGISTER_DECODER(RMB)
REGISTER_DECODER(RMC)
REGISTER_DECODER(GPSCNAVRAWMESSAGE)
REGISTER_DECODER(GPSEPHEM)
REGISTER_DECODER(VTG)
REGISTER_DECODER(ZDA)
REGISTER_DECODER(ALIGNBSLNENU)
REGISTER_DECODER(ALIGNBSLNXYZ)
REGISTER_DECODER(ALIGNDOP)
REGISTER_DECODER(ALMANAC)
REGISTER_DECODER(BESTPOS)
REGISTER_DECODER(PSRPOS)
REGISTER_DECODER(TIME)
REGISTER_DECODER(RTKDOP)
REGISTER_DECODER(SBAS25)
REGISTER_DECODER(BESTSATS)
REGISTER_DECODER(SATXYZ2)
REGISTER_DECODER(BDSEPHEMERIS)
REGISTER_DECODER(AUTHCODES)
REGISTER_DECODER(AVEPOS)
REGISTER_DECODER(BESTVEL)
REGISTER_DECODER(PSRDOP2)
REGISTER_DECODER(RAIMSTATUS)
} // namespace GnssLogs